//
//  YAYIMutiPracticeViewController.m
//  YAYIDoctor
//
//  Created by Veer on 16/6/21.
//
//

#import "YAYIMutiPracticeViewController.h"

#import "OUNavigationController.h"
#import "YSLDraggableCardContainer.h"
#import "YAYIMutiPracticeDetailViewController.h"
#import "YAYILoginViewController.h"
#import "YAYICertificationViewController.h"
#import "YAYIAddResumeController.h"

#import "CardView.h"
#import "YAYIMutiPracticeHospitalCell.h"
#import "YAYIRecruitHospitalModel.h"



#define RGB(r, g, b)	 [UIColor colorWithRed: (r) / 255.0 green: (g) / 255.0 blue: (b) / 255.0 alpha : 1]

// alertTag
#define AlertNotLoginTag 600
#define AlertNoVerticalTag 100
#define AlertNoResumeTag 101


@interface YAYIMutiPracticeViewController () <YSLDraggableCardContainerDelegate, YSLDraggableCardContainerDataSource, YAYIMutiPracticeDetailViewControllerDelegate, UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>

@property (nonatomic, strong) YSLDraggableCardContainer *container;
@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, weak) UILabel *flagLbl;

@property (nonatomic, strong) NSMutableArray *datas;
@property (nonatomic, strong) NSMutableArray *recruitHospitalArray;
@property (nonatomic, assign) NSInteger curCellCount;
@property (nonatomic, assign) NSInteger isShowAnimation;
@property (nonatomic, assign) NSInteger isShowTableView;


/** 我的简历标示  myResumeV:审核状态 myResumeState:是否提交 */
//@property (nonatomic, assign) NSInteger myResumeV;
@property (nonatomic, assign) NSInteger myResumeState;
@property (nonatomic, assign) NSInteger myVerifyState;
/** 保存当前喜欢的index */
@property (nonatomic, assign) NSInteger curSelectedIndex;

/**
 *  分页相关
 */
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, assign) NSInteger pageCount;

@end

@implementation YAYIMutiPracticeViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"点点医生";
    self.view.backgroundColor = [UIColor whiteColor];//RGB(235, 235, 235);
    
    self.isShowAnimation = YES;
    self.recruitHospitalArray = [NSMutableArray array];
//    self.myResumeV = -1;
    self.myResumeState = -1;
    self.curSelectedIndex= -1;
    
    [self setupNav];
    [self setupNoDataFlag];
    [self setupSubView];
    [self getRecruitHospitalList];
    [self getResumeState];
    [self getUserInfo];
    // 处理tableview
    [self setupTableView];
}


- (void)setupNav
{
    
    UIButton *backBtn =  (UIButton *)self.navigationItem.leftBarButtonItem.customView;
    [backBtn removeTarget:self.navigationController action:nil forControlEvents:UIControlEventTouchUpInside];
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"changelistIcon1"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"changelistIcon2"] forState:UIControlStateSelected];
    
    button.frame = CGRectMake(0, 0, 50, 20);
    
    // 让按钮内部的所有内容右对齐
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [button addTarget:self action:@selector(changeList:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}


- (void)setupSubView
{
    
    CGFloat btnWH = 90 * YAYIScreenScale;
    CGFloat btnY = SCREEN_height - btnWH - 20;
    
    _container = [[YSLDraggableCardContainer alloc]init];
    _container.frame = CGRectMake(0, 0, SCREEN_width, btnY);
//    _container.backgroundColor = [UIColor whiteColor];
    _container.dataSource = self;
    _container.delegate = self;
    _container.canDraggableDirection = YSLDraggableDirectionLeft | YSLDraggableDirectionRight;
    [self.view addSubview:_container];
    
    
    CGFloat btnMargin = (SCREEN_width - 30 - 2 * btnWH) / 4.0;
    for (int i = 0; i < 2; i++) {
        
        CGFloat btnX = 15 + btnMargin * (2 * i + 1) + btnWH * i;
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(btnX, btnY, btnWH, btnWH);
        button.clipsToBounds = YES;
        button.layer.cornerRadius = button.frame.size.width / 2;
        button.tag = i;
        [button addTarget:self action:@selector(onLikeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view insertSubview:button atIndex:0];
        
        if (i == 0) { [button setImage:[UIImage imageNamed:@"unLikeBtn"] forState:UIControlStateNormal]; }
        if (i == 1) { [button setImage:[UIImage imageNamed:@"likeBtn"] forState:UIControlStateNormal]; }
    }
    
    [_container reloadCardContainer];
}


/**
 *  没有数据时显示
 */
- (void)setupNoDataFlag
{

    UILabel *flagLbl = [[UILabel alloc] init];
    flagLbl.text = @"最后一页了哦";
    flagLbl.font = [UIFont systemFontOfSize:16];
    flagLbl.textColor = YAYIColor(107, 178, 242);
    flagLbl.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:flagLbl];
    self.flagLbl = flagLbl;
    [flagLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view).offset(-50);
    }];
}



- (void)setupTableView
{

    UITableView *tableView = [[UITableView alloc] init];
    tableView.backgroundColor = YAYIColorAlpha(255, 255, 255, 0);
    tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.hidden = YES;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.frame = self.view.bounds;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    tableView.tableFooterView = [[UIView alloc] init];
    
    __block typeof(self) weakself = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [weakself getRecruitHospitalList];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [weakself getMoreRecruitHospital];
    }];
}


#pragma mark - 获取数据
- (void)getUserInfo
{
    // 显示菊花
    //    [SVProgressHUD show];
    
    [YAYINetWorking getWithURL:me_personalinfo param:nil finish:^(NSData *data, NSDictionary *dict, NSError *error) {
        if (error == nil) {
            
            YAYILog(@"返回：%@", dict);
            
            if (dict == nil || [dict isKindOfClass:[NSNull class]] || ![dict[@"status"] isEqualToString:@"success"]) {
                
                self.myVerifyState = 0;
                if (self.curSelectedIndex > -1) {
                    [self sendResume:self.curSelectedIndex];
                }
            }else{
                // 认证标志(0、未提交，1、待处理，2、已认证，3、认证失败)
                self.myVerifyState = [dict[@"data"][@"v"] integerValue];
                
                if (self.curSelectedIndex > -1) {
                    [self sendResume:self.curSelectedIndex];
                }
            }
        }
        
        [SVProgressHUD dismiss];
    }];
    
}


- (void)getResumeState
{
    [YAYINetWorking getWithURL:resumeInfo_url param:nil finish:^(NSData *data, NSDictionary *dict, NSError *error) {
        
        YAYILog(@"返回:%@", dict);
        
        if (error == nil) {
            
            if ([dict[@"status"] isEqualToString:@"success"]) {
                // "v": 简历审核标志（0：审核中；1：通过审核；2：审核失败）
                self.myResumeState = [dict[@"map"][@"state"] integerValue];
            }
        }
    }];
}



- (void)getRecruitHospitalList
{
    
    [self.recruitHospitalArray removeAllObjects];

    NSMutableDictionary *param = [NSMutableDictionary dictionary];
//    param[@"currentPage"] = @(self.currentPage + 1);
    NSDictionary *locDcit = [[NSUserDefaults standardUserDefaults] dictionaryForKey:YAYILocationKey];
    if (locDcit) {
        param[@"pointX"] = locDcit[@"longitude"];
        param[@"pointY"] = locDcit[@"latitude"];
    }
    
    YAYILog(@"param:%@", param);
    
    [YAYINetWorking getWithURL:docPpHosList_url param:param finish:^(NSData *data, NSDictionary *dict, NSError *error) {
        
        YAYILog(@"返回:%@", dict);
        
        
        if ([dict[@"status"] isEqualToString:@"redirect"]) { // 未登录
            
            self.flagLbl.text = @"没有登录哦";
            
            UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"" message:@"您还没有登录哦，是否现在去登录?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            alerView.tag = AlertNotLoginTag;
            [alerView show];
            return;
        }
        
        if ([dict[@"status"] isEqualToString:@"success"]) {
            
            self.flagLbl.text = @"最后一页哦";
            
            NSArray *dataArray = dict[@"data"];
            
            // 保存分页数据
            self.currentPage = [dict[@"map"][@"page"][@"currentPage"] integerValue];
            self.pageCount = [dict[@"map"][@"page"][@"totalPage"] integerValue];
            
            for (NSDictionary *itemDict in dataArray) {
                
                YAYIRecruitHospitalModel *model = [[YAYIRecruitHospitalModel alloc] init];
                [model setValuesForKeysWithDictionary:itemDict];
                [self.recruitHospitalArray addObject:model];
            }
            
            if (self.tableView.hidden) {
                
                [self.container reloadCardContainer];
            }else{
                
                self.curCellCount = self.recruitHospitalArray.count;
                if (self.currentPage == self.pageCount) {
                    [self.tableView.mj_footer endRefreshing];
                    self.tableView.mj_footer.hidden = YES;
                }else{
                    [self.tableView.mj_footer setState:MJRefreshStateIdle];
                    self.tableView.mj_footer.hidden = NO;
                }
                [self.tableView.mj_header endRefreshing];
                [self.tableView reloadData];
            }
        }
    }];
}


- (void)getMoreRecruitHospital
{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"currentPage"] = @(self.currentPage + 1);
    
    NSDictionary *locDcit = [[NSUserDefaults standardUserDefaults] dictionaryForKey:YAYILocationKey];
    if (locDcit) {
        param[@"pointX"] = locDcit[@"longitude"];
        param[@"pointY"] = locDcit[@"latitude"];
    }
    
    YAYILog(@"param:%@", param);
    
    [YAYINetWorking getWithURL:docPpHosList_url param:param finish:^(NSData *data, NSDictionary *dict, NSError *error) {
        
        YAYILog(@"返回:%@", dict);
        
        if ([dict[@"status"] isEqualToString:@"success"]) {
            NSArray *dat = dict[@"data"];
            
            // 保存分页数据
            self.currentPage = [dict[@"map"][@"page"][@"currentPage"] integerValue];
            self.pageCount = [dict[@"map"][@"page"][@"totalPage"] integerValue];
            
            for (NSDictionary *itemDict in dat) {
                
                YAYIRecruitHospitalModel *model = [[YAYIRecruitHospitalModel alloc] init];
                [model setValuesForKeysWithDictionary:itemDict];
                [self.recruitHospitalArray addObject:model];
            }
            
            if (!self.tableView.hidden) {
                
                self.curCellCount = self.recruitHospitalArray.count;
                if (self.currentPage == self.pageCount) {
//
                    [self.tableView.mj_footer endRefreshing];
                    self.tableView.mj_footer.hidden = YES;
                }else{
                    [self.tableView.mj_footer endRefreshing];
                }
                [self.tableView reloadData];
            }
        }
    }];
}


- (void)sendResume:(NSInteger)index
{
    if (self.myVerifyState == -1) {
        
        self.curSelectedIndex = index;
        return;
    }else{  // 认证标志(0、未提交，1、待处理，2、已认证，3、认证失败)
    
        if (self.myVerifyState == 0) { // 未提交
            
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:@"您还未提交认证，现在去提交？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去提交", nil];
            alertView.tag = AlertNoVerticalTag;
            [alertView show];
            return;
        }else if (self.myVerifyState == 1) { // 待处理
        
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:@"您认证信息正在审核中，请耐心等待" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            alertView.tag = AlertNoVerticalTag;
            [alertView show];
            return;
        }else if (self.myVerifyState == 3) { // 认证失败
            
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:@"您的认证失败，是否重新提交？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去提交", nil];
            alertView.tag = AlertNoVerticalTag;
            [alertView show];
            return;
        }
    }
    
    // 判断是否提交简历
    if (self.myResumeState == 0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:@"你还未添加个人简历，不能投递简历，现在去添加？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去添加", nil];
        alertView.tag = AlertNoResumeTag;
        [alertView show];
        return;
    }
    
    YAYIRecruitHospitalModel *model = self.recruitHospitalArray[index];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"jobid"] = @(model.jobid);
    
    YAYILog(@"param:%@", param);
    
    
    [YAYINetWorking postJsonSerilizeWithURL:sendResume_url param:param finish:^(NSData *data, NSDictionary *dict, NSError *error, NSDictionary *responseError) {
        
        YAYILog(@"投递简历返回：%@", dict);
        
        if (nil == error) {
            
            if ([dict[@"status"] isEqualToString:@"success"]) {
                
                // map存放是否填写简历状态（state：0未填写；state：1:已经填写；state：2：已经投递过简历）
                // 判断简历及认证信息提交状态
                NSInteger resumeState = [dict[@"map"][@"state"] integerValue];
                
                if (resumeState == 0) {  // 未填写简历
            
                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:@"你还未添加个人简历，不能投递简历，现在去添加？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去添加", nil];
                    alertView.tag = AlertNoResumeTag;
                    [alertView show];
                }else if (resumeState == 2){
                    
                    [SVProgressHUD showImage:nil status:@"已投过简历"];
                    if (self.myResumeState == -1) {
                        [self.container movePositionWithDirection:YSLDraggableDirectionRight isAutomatic:NO];
                    }
                }else if (self.myResumeState == -1){
                
                    [self.container movePositionWithDirection:YSLDraggableDirectionRight isAutomatic:NO];
                }
                
                if (self.myResumeState == -1) {
                    self.myResumeState = resumeState > 0 ? 1 : 0;
                }
//                [SVProgressHUD showSuccessWithStatus:@"投递成功"];
            }
        }else{
            YAYILog(@"responseError:%@", responseError);
        }
    }];
}





#pragma mark - 点击事件
- (void)back
{

    if (!self.tableView.hidden) {
        
        UIButton *rightBtn = self.navigationItem.rightBarButtonItem.customView;
        rightBtn.selected = NO;
        
        self.isShowTableView = NO;
        self.isShowAnimation = YES;
        
        [self.tableView reloadData];
        
        [UIView animateWithDuration:0.5 delay:0.1 options:0 animations:^{
            
            self.tableView.backgroundColor = [UIColor colorWithWhite:0.965 alpha:0];
        } completion:^(BOOL finished) {
            
            // 这里置为0方便下次切换，初始化tableview
            self.tableView.hidden = YES;
            self.curCellCount = 0;
            [self.tableView reloadData];
        }];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)onLikeBtnClick:(UIButton *)button
{
    
    [self showOscillatoryAnimationWithLayer:button.layer type:YES];
    if (button.tag == 0) { // 不喜欢
        
        NSInteger index = [_container getCurrentIndex];
        
        if (index >= self.recruitHospitalArray.count) {
            [SVProgressHUD showImage:nil status:@"最后一页了哦"];
            return;
        }
        
        // 判断当剩余4页时，加载下一页
        if (self.recruitHospitalArray.count - index - 1 == 4 && self.currentPage < self.pageCount) {
            // 加载下一页
            [self getMoreRecruitHospital];
        }
        
        [_container movePositionWithDirection:YSLDraggableDirectionLeft isAutomatic:YES];
    }
    if (button.tag == 1) { // 喜欢

        NSInteger index = [_container getCurrentIndex];
        
        if (index >= self.recruitHospitalArray.count) {
            [SVProgressHUD showImage:nil status:@"最后一页了哦"];
            return;
        }
        
        __block typeof(self) weakself = self;
        
        if (self.myVerifyState == 2 && self.myResumeState > 0) {
            [self sendResume:index];
            [_container movePositionWithDirection:YSLDraggableDirectionRight isAutomatic:YES];
        }else{
            [_container movePositionWithDirection:YSLDraggableDirectionRight isAutomatic:YES undoHandler:^{
                [weakself sendResume:index];
            }];
        }
    }
}


- (void)showOscillatoryAnimationWithLayer:(CALayer *)layer type:(BOOL)type{
    NSNumber *animationScale1 = type ? @(1.15) : @(0.5);
    NSNumber *animationScale2 = type ? @(0.92) : @(1.15);
    
    [UIView animateWithDuration:0.15 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
        [layer setValue:animationScale1 forKeyPath:@"transform.scale"];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.15 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
            [layer setValue:animationScale2 forKeyPath:@"transform.scale"];
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
                [layer setValue:@(1.0) forKeyPath:@"transform.scale"];
            } completion:nil];
        }];
    }];
}


- (void)changeList:(UIButton *)sender
{
    sender.selected = !sender.selected;
    
    // self.recruitHospitalArray.count
    if (sender.selected) {
        
        self.curCellCount = self.recruitHospitalArray.count;
        self.isShowTableView = YES;
        self.isShowAnimation = YES;
        self.tableView.hidden = NO;
        [self.tableView reloadData];
        
        [UIView animateWithDuration:0.3 delay:0.3 options:0 animations:^{
            
            self.tableView.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1];
        } completion:nil];
    }else{
        
        self.isShowTableView = NO;
        self.isShowAnimation = YES;
        
        [self.tableView reloadData];
        
        [UIView animateWithDuration:0.5 delay:0.1 options:0 animations:^{
            
            self.tableView.backgroundColor = [UIColor colorWithWhite:0.965 alpha:0];
        } completion:^(BOOL finished) {
            
            // 这里置为0方便下次切换，初始化tableview
            self.tableView.hidden = YES;
            self.curCellCount = 0;
            [self.tableView reloadData];
        }];
    }
}


#pragma mark - 代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.curCellCount;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat h1 = [[UIFont systemFontOfSize:17] lineHeight];
    CGFloat h2 = [[UIFont systemFontOfSize:15] lineHeight];
    return 340 * YAYIScreenScale + h1 + h2 + 25 + 15 + 53;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YAYIRecruitHospitalModel *model = self.recruitHospitalArray[indexPath.row];
    YAYIMutiPracticeHospitalCell *cell = [YAYIMutiPracticeHospitalCell cellWith:tableView];
    cell.model = model;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YAYIMutiPracticeHospitalCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//    CGRect rect1 = [cell.imgView frameForAlignmentRect:self.view.bounds];
    CGRect rect2 = [cell.imgView convertRect:cell.imgView.frame toView:self.view];
    YAYILog(@"rect1:%@  rect2:%@", NSStringFromCGRect(cell.imgView.frame), NSStringFromCGRect(rect2));
    
    CGRect rect =  CGRectMake(0, 64, SCREEN_width, cell.imgView.height);
    
    YAYIRecruitHospitalModel *model = self.recruitHospitalArray[indexPath.row];
    // push方式
    YAYIMutiPracticeDetailViewController *vc = [[YAYIMutiPracticeDetailViewController alloc] initWithModel:cell.imgView.image desImageViewRect:rect];
    vc.delegate = self;
//    vc.recruitid = [NSString stringWithFormat:@"%ld", model.recruitid];
    vc.recruitHospitalModel = model;
    [((OUNavigationController*)self.navigationController) pushViewController:vc withImageView:cell.imgView desRect:rect delegate:vc];
}


-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static CGFloat initialDelay = 0.2f;
    static CGFloat stutter = 0.06f;
    
    if (self.isShowTableView) {
        
        if (self.isShowAnimation) {
            YAYIMutiPracticeHospitalCell *cardCell = (YAYIMutiPracticeHospitalCell *)cell;
            [cardCell startAnimationWithDelay:initialDelay + ((indexPath.row) * stutter)];
            
            NSInteger showCount = SCREEN_height / (310 * YAYIScreenScale + 32 + 35) + 1;
            if (indexPath.row == showCount - 1) {
                self.isShowAnimation = NO;
            }
        }
    }else{
    
        if (self.isShowAnimation) {
            YAYIMutiPracticeHospitalCell *cardCell = (YAYIMutiPracticeHospitalCell *)cell;
            [cardCell startQuitAnimationWithDelay:initialDelay + ((indexPath.row) * stutter)];
        }
    }
    
}


#pragma mark UIAlertView
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == AlertNotLoginTag) {    // 未登录
        
        if (buttonIndex == 1) {
            YAYILoginViewController *loginView = [YAYILoginViewController new];
            __block typeof(self) weakself = self;
            loginView.loginSuccessOperation = ^(){
                YAYILog(@"登陆成功");
                [weakself getRecruitHospitalList];
//                [weakself getResumeState];
            };
            
            loginView.cancelOperation = ^(){
                YAYILog(@"取消登陆");
                [weakself.navigationController popViewControllerAnimated:YES];
            };
            
            YAYINavigationController *nav = [[YAYINavigationController alloc] initWithRootViewController:loginView];
            [self presentViewController:nav animated:YES completion:nil];
        }
    }else if (alertView.tag == AlertNoVerticalTag) { // 没有认证
    
        if (buttonIndex == 1) {
            
            YAYICertificationViewController *certifyVc = [[YAYICertificationViewController alloc] init];
            __block typeof(self) weakself = self;
            certifyVc.backOperation = ^(){
                weakself.myVerifyState = -1;
                [weakself getUserInfo];
            };
            [self.navigationController pushViewController:certifyVc animated:YES];
        }
        [self.container movePositionWithDirection:YSLDraggableDirectionDefault isAutomatic:YES];
    }else if (alertView.tag == AlertNoResumeTag) {  // 填写简历
        
        if (buttonIndex == 1) {
            
            YAYIAddResumeController *addResumeVc = [[YAYIAddResumeController alloc] init];
            __block typeof(self) weakself = self;
            addResumeVc.backOperation = ^(){
                weakself.myResumeState = -1;
            };
            [self.navigationController pushViewController:addResumeVc animated:YES];
        }
        [self.container movePositionWithDirection:YSLDraggableDirectionDefault isAutomatic:YES];
    }
}


#pragma mark - YSLDraggableCardContainer DataSource
- (UIView *)cardContainerViewNextViewWithIndex:(NSInteger)index
{
//    NSDictionary *dict = _datas[index];
    YAYIRecruitHospitalModel *model = self.recruitHospitalArray[index];
    CardView *view = [[CardView alloc] initWithFrame:CGRectMake(15, 80, self.view.width - 30, 432 * YAYIScreenScale)];
    view.model = model;
    return view;
}



- (NSInteger)cardContainerViewNumberOfViewInIndex:(NSInteger)index
{
    return self.recruitHospitalArray.count;
}

#pragma mark -- YSLDraggableCardContainer Delegate
- (void)cardContainerView:(YSLDraggableCardContainer *)cardContainerView didEndDraggingAtIndex:(NSInteger)index draggableView:(UIView *)draggableView draggableDirection:(YSLDraggableDirection)draggableDirection
{
    if (draggableDirection == YSLDraggableDirectionLeft) { // 不感兴趣
        [cardContainerView movePositionWithDirection:draggableDirection
                                         isAutomatic:NO];
    }
    
    if (draggableDirection == YSLDraggableDirectionRight) { // 感兴趣
//        [cardContainerView movePositionWithDirection:draggableDirection isAutomatic:NO];
        __block typeof(self) weakself = self;
        
        if (self.myVerifyState == 2 && self.myResumeState > 0) {
            [self sendResume:index];
            [cardContainerView movePositionWithDirection:draggableDirection isAutomatic:NO];
        }else{
            [cardContainerView movePositionWithDirection:draggableDirection isAutomatic:NO undoHandler:^{
                [weakself sendResume:index];
            }];
        }
    }
    
    if (draggableDirection == YSLDraggableDirectionUp) { // 不感兴趣
        [cardContainerView movePositionWithDirection:draggableDirection
                                         isAutomatic:NO];
    }
    
    // 判断当剩余4页时，加载下一页
    if (self.recruitHospitalArray.count - index - 1 == 4 && self.currentPage < self.pageCount) {
        // 加载下一页
        [self getMoreRecruitHospital];
    }
}


- (void)cardContainderView:(YSLDraggableCardContainer *)cardContainderView updatePositionWithDraggableView:(UIView *)draggableView draggableDirection:(YSLDraggableDirection)draggableDirection widthRatio:(CGFloat)widthRatio heightRatio:(CGFloat)heightRatio
{
    CardView *view = (CardView *)draggableView;
    
    if (draggableDirection == YSLDraggableDirectionDefault) {
        view.selectedView.alpha = 0;
        
        view.likeView.alpha = 0;
        view.unlikeView.alpha = 0;
    }
    
    if (draggableDirection == YSLDraggableDirectionLeft) {
//        view.selectedView.backgroundColor = RGB(215, 104, 91);
//        view.selectedView.alpha = widthRatio > 0.8 ? 0.8 : widthRatio;
        
        view.likeView.alpha = 0;
//        view.unlikeView.backgroundColor = YAYIColor(215, 104, 91);
        view.unlikeView.alpha = widthRatio * 4 > 1.0 ? 1.0 : widthRatio * 4;
    }
    
    if (draggableDirection == YSLDraggableDirectionRight) {
//        view.selectedView.backgroundColor = RGB(114, 209, 142);
//        view.selectedView.alpha = widthRatio > 0.8 ? 0.8 : widthRatio;
        
        view.unlikeView.alpha = 0;
//        view.likeView.backgroundColor = RGB(114, 209, 142);
        view.likeView.alpha = widthRatio * 4 > 1.0 ? 1.0 : widthRatio * 4;
    }
    
    if (draggableDirection == YSLDraggableDirectionUp) {
//        view.selectedView.backgroundColor = RGB(66, 172, 225);
//        view.selectedView.alpha = heightRatio > 0.8 ? 0.8 : heightRatio;
    }
}

/**
 *  划完所有卡片后执行
 */
- (void)cardContainerViewDidCompleteAll:(YSLDraggableCardContainer *)container;
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        
        [container reloadCardContainer];
    });
}


- (void)cardContainerView:(YSLDraggableCardContainer *)cardContainerView didSelectAtIndex:(NSInteger)index draggableView:(UIView *)draggableView
{
    
    YSLDraggableCardContainer *cardContainer = (YSLDraggableCardContainer *)cardContainerView;
    CardView *cardView = (CardView *)[cardContainer getCurrentView];
    
    CGFloat imgH = SCREEN_width / cardView.imageView.width * cardView.imageView.height;
    CGRect rect =  CGRectMake(0, 64, SCREEN_width, imgH);
    
    YAYIRecruitHospitalModel *model = self.recruitHospitalArray[index];
    // push方式
    YAYIMutiPracticeDetailViewController *vc = [[YAYIMutiPracticeDetailViewController alloc] initWithModel:cardView.imageView.image desImageViewRect:rect];
    vc.delegate = self;
//    vc.recruitid = [NSString stringWithFormat:@"%zd", model.recruitid];
    vc.recruitHospitalModel = model;
    [((OUNavigationController*)self.navigationController) pushViewController:vc withImageView:cardView.imageView desRect:rect delegate:vc];
}


#pragma mark YAYIMutiPracticeDetailViewControllerDelegate
- (void)mutiPracticeDetailDidNextCardCicked
{
    
    NSTimeInterval time = 0;
    if (self.tableView.hidden) {
        time = 0.1;
    }
    
    __block typeof(self) weakself = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        NSInteger index = [self.container getCurrentIndex];
        
        if (weakself.myVerifyState == 2 && weakself.myResumeState > 0) {
            [weakself sendResume:index];
            [weakself.container movePositionWithDirection:YSLDraggableDirectionRight isAutomatic:YES];
        }else{
            [weakself.container movePositionWithDirection:YSLDraggableDirectionRight isAutomatic:YES undoHandler:^{
                [weakself sendResume:index];
            }];
        }
    });
}




@end
